package jmm.threadPool.completionService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CompletionServiceDemo5 {

    public static void main(String[] args) {

        // 创建线程池
        ExecutorService executor =
                Executors.newFixedThreadPool(3);

        List<String> taskList = new ArrayList(5);
        taskList.add("w");
        taskList.add("s");
        taskList.add("C");

        List<Future> futures = new ArrayList<>();

        taskList.stream().forEach(task->{
            Future<String> future = executor.submit(()->{
                System.out.println("开始"+task);
                if (task.equals("s")){
                    Thread.sleep(5000);
                }
                System.out.println("===="+task);
                return task;
            });

            futures.add(future);
        });

        for (Future future : futures) {

            try {
                System.out.println("结果"+future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}
